// Copyright (c) XiGuan 2023
// Author： xiguan
// Email: xiguan.teng@qq.com
// Create on 2023/11/16
// TODO:
//

#ifndef WEBSERVER_HTTP_HTTPCONN_H
#define WEBSERVER_HTTP_HTTPCONN_H

#include <Buffer.h>
#include <HttpRequest.h>
#include <HttpResponse.h>
#include <Log.h>
#include <SqlConnRAII.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

#include <atomic>
#include <cstdlib>

class HttpConn {
 private:
  int fd_;

  struct sockaddr_in addr_;

  bool is_closed_;

  int iov_cnt_;

  struct iovec iov_[2];

  Buffer read_buffer_;

  Buffer write_buffer_;

  HttpRequest request_;
  HttpResponse response_;

 public:
  static bool is_et_model_;
  static const char *src_dir_;
  static std::atomic<int> user_count_;

 public:
  HttpConn();

  ~HttpConn();

  int getFd() const;

  const sockaddr_in &getAddr() const;

  void init(int fd, const sockaddr_in &addr);

  const char *get_ip() const;

  int get_port() const;

  ssize_t read(int *save_errno);

  ssize_t write(int *save_errno);

  int to_write_bytes();

  bool is_keep_alive() const;

  void close();

  bool process();
};

#endif  // WEBSERVER_HTTP_HTTPCONN_H
